<template>
  <div>
    <el-card class="box-card" style="margin:10px 0">
      <!-- slot="header" -->
      <div class="clearfix">
        <!-- 头部左侧内容 -->
        <el-tabs v-model="activeName" class="tab">
          <el-tab-pane label="销售额" name="sale" />
          <el-tab-pane label="访问量" name="visit" />
        </el-tabs>
        <!-- 头部右侧内容 -->
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            value-format="yyyy-MM-dd"
            class="date"
            size="small"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </div>
      </div>
      <div>
        <el-row>
          <el-col :span="18">
            <!-- 准备容器 -->
            <div ref="charts" class="charts" />
          </el-col>
          <el-col :span="6">
            <div class="saleRight">
              <h3>门店{{ title }}排名</h3>
              <ul>
                <li v-for="(rank, index) in userRank" :key="index">
                  <!-- <span class="rindex">1</span>
                  <span class="rcontent">肯德基</span>
                  <span class="rvalue">122</span> -->
                  <!-- <span class="rindex">{{ rank.no }}</span> -->
                  <span :class="rank.no <= 3 ? 'rindex' : 'rindex1'">{{ rank.no }}</span>
                  <span class="rcontent">{{ rank.name }}</span>
                  <span v-if="activeName=='sale'" class="rvalue">{{ rank.money.split(',')[0] }}</span>
                  <span v-else class="rvalue">{{ rank.money.split(',')[1] }}</span>
                </li>
                <!-- <li>
                  <span class="rindex">2</span>
                  <span class="rcontent">麦当劳</span>
                  <span class="rvalue">122</span>
                </li>
                <li>
                  <span class="rindex">3</span>
                  <span class="rcontent">海底捞</span>
                  <span class="rvalue">122</span>
                </li>
                <li>
                  <span class="rindex1">4</span>
                  <span class="rcontent">西贝莜面村</span>
                  <span class="rvalue">122</span>
                </li>
                <li>
                  <span class="rindex1">5</span>
                  <span class="rcontent">汉堡王</span>
                  <span class="rvalue">122</span>
                </li>
                <li>
                  <span class="rindex1">6</span>
                  <span class="rcontent">真功夫</span>
                  <span class="rvalue">122</span>
                </li>
                <li>
                  <span class="rindex1">7</span>
                  <span class="rcontent">淄博烧烤</span>
                  <span class="rvalue">122</span>
                </li> -->
              </ul>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>
<script>
// 引入 echarts
import * as echarts from 'echarts'
import dayjs from 'dayjs'
import { mapState } from 'vuex'
export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      myecharts: null,
      // 收集日历的数据
      date: []
      // 门店排名
      // userRank: []
    }
  },
  computed: {
    // 计算属性-标题
    title() {
      console.log(123)
      return this.activeName === 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: state => state.home.list
    }),
    userRank() {
      return this.listState.userRank
    }
  },
  // 监听属性
  watch: {
    title() {
      console.log('修改配置数据')
      // 重新修改图标的配置数据
      // 图表配置数据可以再次修改，如果有新的数值，用新的数值；没有新的数值，用原先的数值
      this.mycharts.setOption({
        title: {
          text: this.title
        },
        // X轴数据
        xAxis: {
          data:
            this.title === '销售额'
              ? this.listState.orderFullYearAxis
              : this.listState.userFullYearAxis
        },
        // Y轴数据
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data:
              this.title === '销售额'
                ? this.listState.orderFullYear
                : this.listState.userFullYear,
            color: '#007aff'
          }
        ]
      })
    },
    listState() {
      // 要监听数据
      // 设置配置数据
      this.mycharts.setOption({
        title: {
          // text: '销售额趋势'
          // text: `${this.title}趋势`
          text: this.title + '趋势'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            // data: [
            //   '1月',
            //   '2月',
            //   '3月',
            //   '4月',
            //   '5月',
            //   '6月',
            //   '7月',
            //   '8月',
            //   '9月',
            //   '10月',
            //   '11月',
            //   '12月'
            // ],
            // data: [],
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            // data: [10, 52, 200, 334, 390, 330, 220, 110, 98, 167, 123, 44],
            // data: [],
            data: this.listState.orderFullYear,
            color: '#007aff'
          }
        ]
      })
    }
  },
  mounted() {
    // 初始化echarts实例
    this.mycharts = echarts.init(this.$refs.charts)

    // 顶部是mounted：为什么第一次没有数据？因为执行一次没有数据，因此数据不显示，要监听listState数据，放到watch中了
  },
  methods: {
    // 今日
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    // 本周
    setWeek() {
      const start = dayjs()
        .day(1)
        .format('YYYY-MM-DD')
      const end = dayjs()
        .day(7)
        .format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本月
    setMonth() {
      const start = dayjs()
        .startOf('month')
        .format('YYYY-MM-DD')
      const end = dayjs()
        .endOf('month')
        .format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本年
    setYear() {
      const start = dayjs()
        .startOf('year')
        .format('YYYY-MM-DD')
      const end = dayjs()
        .endOf('year')
        .format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>
<style lang="scss">
.clearfix {
  display: flex;
  justify-content: space-between;
  /* 相对定位 */
  position: relative;
}
.tab {
  width: 100%;
}
.right {
  /*  绝对定位 */
  position: absolute;
  right: 0;
}
.right span {
  margin: 0 10px;
}
.date {
  width: 300px !important;
  margin: 0 20px;
}
.charts {
  width: 100%;
  height: 300px;
}
// .saleRight {
//   // padding: 0;
//   // margin: 0
// }
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
ul li {
  height: 8%;
  margin: 10px 0;
}
.rindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
}
.rindex1 {
  float: left;
  width: 20px;
  height: 20px;
  //   border-radius: 50%;
  //   background: black;
  color: black;
  text-align: center;
}
.rcontent {
  margin-left: 30px;
}
.rvalue {
  float: right;
}
</style>
